var app = getApp()
Page({
    data: {
        code: 0,
        fuid: 0,
        tab: 1,
        idcardfront: '',
        idcardback: '',
        drivingcard: '',
        setsize: 2.4, //上传图片大小M
        student: 0,
        ispass: 0,
        showpanl: false,
        showsign: false,
        showkefu: false,
    },

    onLoad(options) {
        var that = this

        //获取海报或分享得到推荐人
        if (options.scene || options.fuid) {
            const query = app.sceneToObject(options.scene)
            // console.log(query.fuid)
            that.setData({
                fuid: query.fuid || options.fuid
            })
        }

        const userinfo = tt.getStorageSync('userinfo')
        if (!userinfo) {
            app.wxlogin().then(function (res) {
                that.setData({
                    userinfo: res,
                    tab: options.tab || 1
                },() => {
                    that.getdata()
                })
            })
        } else {
            that.setData({
                userinfo,
                tab: options.tab || 1
            },() => {
                that.getdata()
            })
        }
    },

    getdata() {
        var that = this
        const fuid = that.data.fuid
        const { uid,openid } = this.data.userinfo
        const data = { uid,openid }
        app.api_request('?c=signup','GET',data).then(function (res) {
            if (res) {
                that.setData({
                    code: 1,
                    pagedata: res,
                    rname: res.rname,
                    idcard: res.idcard,
                    address: res.address,
                    idcarddate1: res.idcarddate1,
                    idcarddate2: res.idcarddate2,
                    idcardfront_filename: res.idcardfront[0],
                    idcardfront: res.idcardfront[1],
                    idcardback_filename: res.idcardback[0],
                    idcardback: res.idcardback[1],
                    drivingcard_filename: res.drivingcard[0],
                    drivingcard: res.drivingcard[1],
                    signfile_filename: res.signfile[0],
                    signfile: res.signfile[1],
                    entryfee_yj: res.entryfee_yj,
                    entryfee: res.entryfee,
                    entryfee1: parseFloat(res.entryfee - res.disfee).toFixed(2),
                    entryfee2: res.entryfee_def_dfee,
                    entryfee3: res.entryfee_lp1_sfee,
                    disfee: res.disfee,
                    disfee_student: res.disfee_student,
                    student: res.student,
                    fuid: fuid >= 0 ? fuid : res.fuid,

                    listc: res.listc,
                    listdef: res.listdef,
                    listlp: res.listlp,
                    listlp_xm: res.listlp_xm,
                    listlp_level: res.listlp_level,

                    entryfee_def_sfee: res.entryfee_def_sfee, //DEF单科费用
                    entryfee_def_dfee: res.entryfee_def_dfee, //DEF双科费用
                    entryfee_lp1_sfee: res.entryfee_lp1_sfee, //单科60分以上费用
                    entryfee_lp1_dfee: res.entryfee_lp1_dfee, //双科60分以上费用
                    entryfee_lp2_sfee: res.entryfee_lp2_sfee, //单科60分以下费用
                    entryfee_lp2_dfee: res.entryfee_lp2_dfee //双科60分以下费用
                })

                tt.setNavigationBarTitle({
                    title: res.appTitle
                })
            }
        })
    },

    setTab(e) {
        this.setData({
            tab: e.target.dataset.tab
        })
    },

    chooseImage(e) {
        var that = this
        tt.chooseImage({
            count: 1,
            sizeType: ['original','compressed'], // original 原图，compressed 压缩图
            sourceType: ['album','camera'], //图片与相机选择的来源
            camera: 'back',//使用前置或后置摄像头
            success: (res) => {
                const upsize = res.tempFiles[0].size //上传大小
                const maxsize = that.data.setsize * 1000 * 1000 //限定大小
                //console.log(maxsize)
                //console.log(upsize)
                // console.log(res)
                if (res.errMsg == 'chooseImage:ok') {
                    if (upsize > maxsize) {
                        tt.showToast({
                            title: `上传文件需<${that.data.setsize}M`,
                            icon: 'none'
                        })
                    } else {
                        let file = res.tempFiles[0].path
                        const { ctype,exfile } = e.currentTarget.dataset
                        const { uid } = that.data.userinfo
                        const data = { uid,exfile }
                        // console.log(file)
                        // console.log(ctype)
                        // console.log(data)
                        app.api_upload('?ctype=' + ctype,file,data).then(function (res) {
                            if (res) {
                                // console.log(res)
                                that.setData({
                                    [ctype + '_filename']: res.filename,
                                    [ctype]: res.filepath,
                                    rname: res.rname,
                                    idcard: res.idcard,
                                    address: res.address,
                                    idcarddate1: res.idcarddate1,
                                    idcarddate2: res.idcarddate2,
                                    disfee: res.disfee,
                                    entryfee: res.entryfee,
                                    entryfee1: parseFloat(res.entryfee - res.disfee).toFixed(2),
                                    student: 0
                                })
                            }
                        })
                    }
                } else {
                    tt.showToast({
                        title: `上传错误`,
                        icon: 'none'
                    })
                }
            }
        })
    },

    previewImage: function (e) {
        let src = e.currentTarget.dataset.src
        // console.log(src)
        tt.previewImage({
            current: src,
            urls: [src]
        })
    },

    radiobox: function (e) {
        let index = e.currentTarget.dataset.index
        let name = e.currentTarget.dataset.name
        let List = this.data[name]
        for (let i = 0; i < List.length; i++) {
            this.data[name][i].checked = false
        }
        List[index].checked = !List[index].checked
        this.setData({
            [name]: this.data[name]
        },() => {
            this.lpfee() //理论培训获取费用
        })
    },

    lpfee() {
        if (this.data.listlp_xm[0].checked && this.data.listlp_level[0].checked) {
            var fee = this.data.entryfee_lp1_sfee
        } else if (this.data.listlp_xm[0].checked && this.data.listlp_level[1].checked) {
            var fee = this.data.entryfee_lp2_sfee
        } else if (this.data.listlp_xm[1].checked && this.data.listlp_level[0].checked) {
            var fee = this.data.entryfee_lp1_dfee
        } else if (this.data.listlp_xm[1].checked && this.data.listlp_level[1].checked) {
            var fee = this.data.entryfee_lp2_dfee
        }
        var disfee = 0
        this.setData({
            entryfee3: fee - disfee
        })
    },

    student() {
        var that = this
        const { entryfee,entryfee_yj,disfee,disfee_student,student } = that.data
        if (entryfee == entryfee_yj && student == 0) {
            tt.showModal({
                content: '已享受最低价，不可再叠加优惠',
                showCancel: false,
                success: function (res) {
                    if (res.confirm) {
                        that.setData({
                            student: 0
                        })
                    }
                }
            })
        } else {
            if (student == 0) {
                tt.showModal({
                    // title: '提示',
                    content: '请再次确认你是学生且持有有效学生证，如不符合条件将按规定补差价',
                    success: function (res) {
                        if (res.confirm) {
                            that.setData({
                                student: 1,
                                disfee: disfee + disfee_student,
                                entryfee1: parseFloat(entryfee - (disfee + disfee_student)).toFixed(2)
                            })
                        } else if (res.cancel) {
                            that.setData({
                                student: false
                            })
                        }
                    }
                })
            } else {
                that.setData({
                    student: 0,
                    disfee: disfee - disfee_student,
                    entryfee1: parseFloat(entryfee - (disfee - disfee_student)).toFixed(2)
                })
            }
        }
    },

    ispass() {
        var that = this
        if (!that.data.ispass) {
            tt.showModal({
                // title: '提示',
                content: '请再次确认你摩托车科目二或科目三单项合格，如不符合条件将按规定补差价',
                success: function (res) {
                    if (res.confirm) {
                        that.setData({
                            ispass: 1,
                            entryfee2: that.data.entryfee_def_sfee
                        })
                    } else if (res.cancel) {
                        that.setData({
                            ispass: 0,
                            entryfee2: that.data.entryfee_def_dfee
                        })
                    }
                }
            })
        } else {
            that.setData({
                ispass: 0,
                entryfee2: that.data.entryfee_def_dfee
            })
        }
    },

    showpanl() {
        this.setData({
            showpanl: true
        })
    },

    showsign() {
        this.setData({
            showsign: true
        })
    },

    showkefu() {
        this.setData({
            showkefu: true
        })
    },

    closeAny() {
        this.setData({
            showpanl: false,
            showsign: false,
            showkefu: false
        })
    },

    UserPrivacyBtn() {
        this.setData({
            accept: !this.data.accept
        })
    },

    submitForm1(e) {
        var that = this
        const data = e.detail.value
        const { uid,openid } = that.data.userinfo
        const { idcarddate1,idcarddate2,student,signfile_filename,entryfee,disfee } = that.data
        data.m_id = that.data.pagedata.id
        data.m_uid = uid
        data.m_openid = openid
        data.m_idcarddate1 = idcarddate1
        data.m_idcarddate2 = idcarddate2
        data.m_student = student
        data.m_signfile = signfile_filename
        data.m_entryfee = entryfee
        data.m_disfee = disfee
        data.m_accept = that.data.accept
        // console.log(data)
        app.api_request('?c=signup_submit1','POST',data).then(function (res) {
            tt.showModal({
                //title: '提示',
                content: res.tipmsg,
                showCancel: false,
                success: function () {
                    if (res.state == 1) {
                        tt.navigateTo({
                            url: '/pages/signup/pay'
                        })
                    }
                }
            })
        })
    },

    submitForm2(e) {
        var that = this
        const data = e.detail.value
        const { uid,openid } = that.data.userinfo
        const { idcarddate1,idcarddate2,signfile_filename,entryfee2,disfee } = that.data
        data.m_id = that.data.pagedata.id
        data.m_uid = uid
        data.m_openid = openid
        data.m_idcarddate1 = idcarddate1
        data.m_idcarddate2 = idcarddate2
        data.m_signfile = signfile_filename
        data.m_entryfee = entryfee2
        data.m_disfee = disfee
        data.m_accept = that.data.accept
        // console.log(data)
        app.api_request('?c=signup_submit2','POST',data).then(function (res) {
            tt.showModal({
                //title: '提示',
                content: res.tipmsg,
                showCancel: false,
                success: function () {
                    if (res.state == 1) {
                        tt.navigateTo({
                            url: '/pages/signup/pay'
                        })
                    }
                }
            })
        })
    },

    submitForm3(e) {
        var that = this
        const data = e.detail.value
        const { uid,openid } = that.data.userinfo
        const { idcarddate1,idcarddate2,signfile_filename,entryfee3,disfee } = that.data
        data.m_id = that.data.pagedata.id
        data.m_uid = uid
        data.m_openid = openid
        data.m_idcarddate1 = idcarddate1
        data.m_idcarddate2 = idcarddate2
        data.m_signfile = signfile_filename
        data.m_entryfee = entryfee3
        data.m_disfee = disfee
        data.m_accept = that.data.accept
        console.log(data)
        app.api_request('?c=signup_submit3','POST',data).then(function (res) {
            tt.showModal({
                //title: '提示',
                content: res.tipmsg,
                showCancel: false,
                success: function () {
                    if (res.state == 1) {
                        tt.navigateTo({
                            url: '/pages/signup/pay'
                        })
                    }
                }
            })
        })
    },

    onShareAppMessage: function () {
        var that = this
        return {
            title: that.data.pagedata.appTitle,
            desc: that.data.pagedata.appDesc,
            path: '/pages/signup/index?fuid=' + that.data.userinfo.uid
        }
    }
})